import Vue from 'vue'
import VueRouter from 'vue-router'

const Layout=()=>import('@/views/Layout')
// const Layout = (resolve) => require(['@/views/Layout/index.vue'], resolve)

// const Login = (resolve) => require(['@/views/Login/index.vue'], resolve)
const Login=()=>import('@/views/Login')

// const Home = () => import('@/views/Home/index.vue')
const Home = (resolve) => require(['@/views/Home'], resolve)


// import Layout from '@/views/Layout'
// import Login from '@/views/Login'
// import Home from '@/views/Home'
// 产品管理
// import GoodsManage from '@/views/GoodsManage'
// import GoodsList from '@/views/GoodsManage/List'
// import AddGoodItem from '@/views/GoodsManage/List/AddGoodItem'
// import GoodsCategory from '@/views/GoodsManage/Category'

// // 订单管理
// import OrderManage from '@/views/OrderManage'
// import OrderList from '@/views/OrderManage/List'
// import OrderCollect from '@/views/OrderManage/Collect'
// import OrderAudit from '@/views/OrderManage/Audit'

// // 广告管理
// import AdvertManage from '@/views/AdvertManage'
// import AdvertList from '@/views/AdvertManage/List'
// import AdvertCategory from '@/views/AdvertManage/Category'

Vue.use(VueRouter)

// export const baseRoutes=[
   
// ]
let home = {
    path: '/home',
    name: 'home',
    component:Home,
    meta: {
        title: '首页'
    }
}

let routes = [
    {
        path: '/login',
        name: 'login',
        component:Login,
        

    },
    {
        path: '/',
        component:Layout,
        redirect:'/home',
        name:'layout',
        meta: {
            title: '首页',
            isLogin: true
        }
    }
]

// 防止跳转相同路由报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}



const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes

})
import store from '@/store'
// 全局前置路由守卫:初始化执行,每次路由切换时执行
router.beforeEach((to, from, next) => {
    console.log('888',to.matched)
    // 检查即将进入的页面是否已经登录或者没有菜单
    if (to.matched.some(ele => ele.meta.isLogin) || to.matched.length===0){
        console.log('token', store.state.login.userInfo.token);
        // 是否存在登录token
        if (store.state.login.userInfo.token) {
            console.log('dymenu', store.state.dynamicmenu.dymenu);
            if (store.state.dynamicmenu.dymenu.length!=0) {
                // 如果已经生成了动态路由
                next()
            }else{
                // 否则
                store.dispatch('dynamicmenu/updateDyMenu').then(res => {
                    // router.addRoutes(res);
                    console.log('3333',res);
                    // res.push(home);
                    console.log(444,home);
                    router.addRoute('layout',home);
                    res.forEach((item) => {
                        router.addRoute('layout',item);
                       
                    });
                    console.log('...', router.getRoutes());
                    next({ ...to, replace: true });
                }).catch(err => {
                    console.log(err);
                })
            }
                
        }else{
            next('/login');
        }
    } else {
      next();
    }
}
)

// router.addRoute('layout',)

export default router;